﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BikeManageBusiness.logical;
using Data.Common;
using Data.Enum;
using Data.Model;
using Data.ViewModel;
using DataAccess.AccountManage;
using DataAccess.BikeManage.logical;
using Ninject;
using WinForm.Helper;

namespace WinForm.UserManage
{
    public partial class ModifyPasswordFrm : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly IAccount _account = Kernel.Get<IAccount>();
        private readonly IBusinesses _business = Kernel.Get<IBusinesses>();

        public ModifyPasswordFrm()
        {
            InitializeComponent();
        }

        private void RefreshData()
        {
            GC.Collect();
            string errorString = string.Empty;
            ResultInfo<PagingDataSet<jsh_account>> accounts = _account.GetAccounts(1, Int32.MaxValue, null,
                                                                                           textBoxX1.Text, null);
            if (accounts.ResultNumber == ResultEnum.Success && accounts.Obj != null)
            {
                try
                {
                    int searil = 0;
                    List<AccountView> views = new List<AccountView>();
                    foreach (jsh_account a in accounts.Obj.Records)
                    {
                        searil++;
                        views.Add(new AccountView()
                        {
                            CpuNo = a.cpuno,
                            BeginDate = Converts.ConvertToNormalDate(a.khdate),
                            CurrentMoney = a.balance,
                            IdCardType = a.zzlx,
                            IdNo = a.zzhm,
                            Phone = (string.IsNullOrEmpty(a.telephone) ? a.mobile : a.telephone),
                            Sex = a.sex,
                            UserName = a.username,
                            ValideDate = Converts.ConvertToNormalDate(a.validate),
                            Serial = searil,
                            Id = a.id,
                            Status = a.status
                        });
                    }
                    dataGridView1.DataSource = views;

                    tabItem1.Visible = false;
                    tabItem2.Visible = true;
                }
                catch (Exception)
                {
                    dataGridView1.DataSource = accounts.Obj.Records;
                }
            }
            else
            {
                errorString += accounts.Describe;
            }
            GC.Collect();
            if (!string.IsNullOrEmpty(errorString))
            {
                MyMessageBox.ShowMessageBoxInfo(errorString);
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            string errorString = string.Empty;
            try
            {
                GC.Collect();
                Dkqa16dLogical dkqa16DLogical = new Dkqa16dLogical();
                ResultInfo<IDCardData> iddata = dkqa16DLogical.GetIdCardData();
                if (iddata.ResultNumber == ResultEnum.Success)
                {
                    textBoxX1.Text = iddata.Obj.IDCardNo;
                }

                if (string.IsNullOrEmpty(textBoxX1.Text))
                {
                    errorString += "  请将身份证放入读卡器或者填入身份证号码！";
                }
                else
                {
                    RefreshData();
                }
            }
            catch (Exception e1)
            {
                errorString += e1.Message;
            }
            GC.Collect();
            if (!string.IsNullOrEmpty(errorString))
            {
                MyMessageBox.ShowMessageBoxInfo(errorString);
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            tabItem1.Visible = true;
            tabItem2.Visible = false;
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                GC.Collect();
                if (dataGridView1.SelectedRows.Count != 1)
                {
                    errorMessage = errorMessage + "必须且只能选择一条数据进行操作";
                }
                else
                {
                    long accountid = Convert.ToInt64(dataGridView1.SelectedRows[0].Cells["id"].Value);
                    labCpuNo.Text = accountid.ToString();
                    tabItem3.Visible = true;
                    tabItem2.Visible = false;
                }
            }
            catch (Exception e1)
            {
                errorMessage += e1.Message;
            }
            GC.Collect();
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        private void buttonX5_Click(object sender, EventArgs e)
        {
            tabItem3.Visible = false;
            tabItem2.Visible = true;
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                GC.Collect();
                if(string.IsNullOrEmpty(labCpuNo.Text))
                {
                    errorMessage += " 请选择需要更新密码的卡！";
                }
                else
                {
                    if(string.IsNullOrEmpty(txbNewPwd.Text)||string.IsNullOrEmpty(txbNewPwdAgin.Text)||(txbNewPwd.Text!=txbNewPwdAgin.Text))
                    {
                        errorMessage += " 输入的密码为空或者两次输入的密码值不同！";
                    }else
                    {
                        ResultInfo<bool> up = _account.UpdateAccountPassword(Converts.ConvertToLong(labCpuNo.Text),
                                                                         txbNewPwd.Text);
                        if (up.ResultNumber == ResultEnum.Success)
                        {
                            errorMessage += "更新成功！";
                        }
                        else
                        {
                            errorMessage += up.Describe;
                        }
                    }
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
            }
            GC.Collect();
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
            Close();
            Dispose();
        }

    }
}
